package com.zprmb.udzt;

import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zprmb.udzt.http.AjaxParams;
import com.zprmb.udzt.ll.BaseHelper;
import com.zprmb.udzt.util.Constant;
import com.zprmb.udzt.util.VolleryUtil;

public class Set_tr_pw extends BaseActivity implements OnClickListener{
	private Button btn_confirm;
	private EditText et_tran_pwd,et_confirm;
	private ImageView img_back,pass,pass2;
	private RelativeLayout rl_back;
	private TextView tv_customer;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.set_transaction_pw);
		 BbtApplication.getInstance().addActvity(this);
		btn_confirm=(Button)findViewById(R.id.btn_confirm);
		img_back=(ImageView)findViewById(R.id.img_back);
		img_back.setOnClickListener(this);
		pass=(ImageView)findViewById(R.id.img_pass);
		pass2=(ImageView)findViewById(R.id.img_pass2);
		et_tran_pwd=(EditText)findViewById(R.id.et_jy_pwd);
		et_confirm=(EditText)findViewById(R.id.et_confirm);
		rl_back=(RelativeLayout)findViewById(R.id.rl_back);
		tv_customer = (TextView) findViewById(R.id.enterprise_tel);
		tv_customer.setOnClickListener(this);
		rl_back.setOnClickListener(this);
		pass.setOnClickListener(this);
		pass2.setOnClickListener(this);
		btn_confirm.setOnClickListener(this);
		btn_confirm.setEnabled(false);
		et_tran_pwd.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				if(et_tran_pwd.getText().toString().equals("")){
					pass.setVisibility(View.GONE);
				}else{
					pass.setVisibility(View.VISIBLE);
				}
			}
		});
		et_confirm.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				if(et_confirm.getText().toString().length()==6&&et_tran_pwd.getText().toString().length()==6){
					
					btn_confirm.setBackgroundResource(R.drawable.v6button);
					btn_confirm.setEnabled(true);
				}else{
					btn_confirm.setBackgroundResource(R.drawable.btn_unclick);
					btn_confirm.setEnabled(false);
				}
				if(et_confirm.getText().toString().equals("")){
					pass2.setVisibility(View.GONE);
				}else{
					pass2.setVisibility(View.VISIBLE);
				}
			}
		});
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		BbtApplication.getInstance().removeActvity(this);
	}
	private void initData() {
		// TODO Auto-generated method stub
		AjaxParams params=new AjaxParams();
		params.put("payPass", et_tran_pwd.getText().toString());
		VolleryUtil volleryUtil=new VolleryUtil() {
			
			@Override
			public void onUpDate(JSONObject jsonObject) {
				// TODO Auto-generated method stub
				Log.e("交易密码",jsonObject.toString());
				if(jsonObject.optInt("success")==1){
					btn_confirm.setBackgroundResource(R.drawable.v6button);
					btn_confirm.setEnabled(true);
					Intent intent=new Intent(Set_tr_pw.this,Updata_Success.class);
					intent.putExtra("title", "设置交易密码");
					intent.putExtra("status", "设置成功");
					startActivity(intent);
					Set_tr_pw.this.finish();
				}else {
					btn_confirm.setBackgroundResource(R.drawable.v6button);
					btn_confirm.setEnabled(true);
					if (jsonObject.optString("comment").equals("please login")) {
						Intent intent = new Intent(Set_tr_pw.this,V6LoginActivity.class);
						startActivity(intent);
					}else{
						Toast.makeText(Set_tr_pw.this, jsonObject.optString("comment"), 2000).show();
					}
				}
			}
			
			@Override
			public void onError() {
				// TODO Auto-generated method stub
				
			}
		};
		volleryUtil.setHeaders("token", getToken());
		volleryUtil.setRequestMethod(1);
		volleryUtil.execute(Set_tr_pw.this, Constant.URL+"/api/v2/setPayPass?", params.toString());
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.img_back:
			finish();
			break;
		case R.id.btn_confirm:
			btn_confirm.setBackgroundResource(R.drawable.btn_unclick);
			btn_confirm.setEnabled(false);
			initData();
			break;
		case R.id.img_pass:
			if(et_tran_pwd.getTransformationMethod().equals(PasswordTransformationMethod  
                    .getInstance())){
				et_tran_pwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
				pass.setBackgroundResource(R.drawable.yan1);
			}else{
				et_tran_pwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
				pass.setBackgroundResource(R.drawable.yan2);
			}
			break;
		case R.id.img_pass2:
			if(et_confirm.getTransformationMethod().equals(PasswordTransformationMethod  
                    .getInstance())){
				et_confirm.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
				pass2.setBackgroundResource(R.drawable.yan1);
			}else{
				et_confirm.setTransformationMethod(PasswordTransformationMethod.getInstance());
				pass2.setBackgroundResource(R.drawable.yan2);
			}
			break;
		case R.id.rl_back:
			finish();
			break;
		case R.id.enterprise_tel:
			BaseHelper.showDialog2(this, "拨号", "400-0101-016", 1);
			break;
		}
	}
	

}
